home *** CD-ROM | disk | FTP | other *** search
-
- /*
- * Vogle driver for Mac II
- *
- */
- #include <stdio.h>
-
- #include <quickdraw.h>
- #include <qdoffscreen.h>
- #include <fcntl.h>
- #include <errno.h>
- #include <math.h>
-
- #include "vogl.h"
-
- #define MIN(x,y) ((x) < (y) ? (x) : (y))
- #define COL(c) ((usememory) ? (c) : (c) | ((c << 4)))
-
-
- /****
- * Globals
- ****/
- static CWindowPtr voglWindow;
- static Rect voglRect;
- static short voglWidth;
- static short voglHeight;
- static GDHandle mainGDevice;
- static GWorldPtr voglWorld;
- static Boolean isScreen;
- static int usememory;
-
-
- /****
- * InitMacintosh()
- *
- * Initialize all the managers & memory
- *
- ****/
-
- void InitMacintosh(void)
-
- {
- MaxApplZone();
-
- InitGraf(&qd.thePort);
- InitFonts();
- FlushEvents(everyEvent, 0);
- InitWindows();
- InitMenus();
- TEInit();
- InitDialogs(0L);
- InitCursor();
-
- }
- /* end InitMacintosh */
-
-
- /*
- * redisplay
- *
- * redisplay the window.
- */
- static void
- redisplay()
- {
- }
-
-
- /*
- * MacII_init
- *
- * initialises drawing window
- */
- MacII_init()
- {
- Rect *wBounds;
- QDErr result;
-
- InitMacintosh();
-
- mainGDevice = GetMainDevice();
- voglWidth = 300;
- voglHeight = 300;
-
- SetRect (&voglRect, 50, 50, voglWidth+50, voglHeight+50);
- voglWindow = (CWindowPtr)NewCWindow(0L, &voglRect, "\pVogl Window", true,
- noGrowDocProc, (WindowPtr) -1L, true, 0);
- ShowWindow ((WindowPtr)voglWindow);
- BringToFront ((WindowPtr)voglWindow);
- SetPort ((WindowPtr)voglWindow);
-
- vdevice.depth = (**(**mainGDevice).gdPMap).pixelSize;
- vdevice.sizeX = vdevice.sizeY = MIN(voglWidth, voglHeight);
- vdevice.sizeSx = voglWidth;
- vdevice.sizeSy = voglHeight;
-
- /* Off screen stuff */
-
- wBounds = &(*((CGrafPtr)voglWindow)->portPixMap)->bounds;
-
- SetOrigin (-wBounds->left, -wBounds->top); /* convert portRect to global */
-
- result = NewGWorld (&voglWorld, 0, &voglWindow->portRect, 0L, 0L, noNewDevice);
-
- SetOrigin (0, 0);
-
- isScreen = TRUE;
-
- /* End of off screen stuff */
-
- return(1);
- }
-
-
- /*
- * MacII_exit
- *
- * cleans up before returning the window to normal.
- */
- MacII_exit()
- {
- }
-
-
- /*
- * MacII_draw
- *
- * draws a line from the current graphics position to (x, y).
- *
- * Note: (0, 0) is defined as the top left of the window on a MacII (easy
- * to forget).
- */
- MacII_draw(x, y)
- int x, y;
- {
- MoveTo (vdevice.cpVx, vdevice.sizeSy - vdevice.cpVy);
- LineTo (x, vdevice.sizeSy - y);
- }
-
-
- /*
- * MacII_getkey
- *
- * grab a character from the keyboard.
- */
- int
- MacII_getkey()
- {
- EventRecord theEvent;
- Boolean done;
- int ch;
-
- done = FALSE;
-
- do {
- if (WaitNextEvent (everyEvent, &theEvent, (short)0, 0L))
- {
- switch (theEvent.what)
- {
- case keyDown: /* Handle key inputs, fall into AutoKey code */
- case autoKey: /* Handle key inputs */
- ch = (int)theEvent.message & (int)charCodeMask; /* Get character pressed */
- done = TRUE;
- break; /* End of doing a keystroke */
- default:
- break;
- }
- }
- } while (!done);
- return(ch);
- }
-
-
- /*
- * MacII_checkkey
- *
- * Check if a keyboard key has been hit. If so return it.
- */
-
- int
- MacII_checkkey()
- {
- EventRecord theEvent;
- int ch;
-
- if (WaitNextEvent (everyEvent, &theEvent, (short)0, 0L))
- {
- switch (theEvent.what)
- {
- case keyDown: /* Handle key inputs, fall into AutoKey code */
- case autoKey: /* Handle key inputs */
- ch = (int)theEvent.message & (int)charCodeMask; /* Get character pressed */
- return(ch);
- break; /* End of doing a keystroke */
- default:
- break;
- }
- }
- return(0);
- }
-
-
- /*
- * MacII_locator
- *
- * return the window location of the cursor, plus which mouse button,
- * if any, is been pressed.
- */
- int
- MacII_locator(wx, wy)
- int *wx, *wy;
- {
- EventRecord theEvent;
- int but;
- Point where;
-
- but = 0;
-
- if (WaitNextEvent (everyEvent, &theEvent, 0, 0L))
- {
- switch (theEvent.what)
- {
- case mouseDown: /* Handle mouse down */
- but |= 1;
- break; /* End of doing a mouseDown */
- default:
- break;
- }
- }
- where = theEvent.where;
- GlobalToLocal (&where);
- *wx = where.h;
- *wy = (int)vdevice.sizeSy - where.v;
- return(but);
- }
-
-
- /*
- * MacII_clear
- *
- * Clear the screen to current colour
- */
- MacII_clear()
- {
- EraseRect (&voglWindow->portRect);
- }
-
-
- /*
- * MacII_color
- *
- * set the current drawing color index.
- */
- MacII_color(ind)
- int ind;
- {
- /*
- RGBColor color;
-
- Index2Color (ind, &color);
- RGBForeColor (&color);
- */
- switch (ind)
- {
- case BLACK:
- ForeColor (blackColor);
- break;
- case RED:
- ForeColor (redColor);
- break;
- case GREEN:
- ForeColor (greenColor);
- break;
- case YELLOW:
- ForeColor (yellowColor);
- break;
- case BLUE:
- ForeColor (blueColor);
- break;
- case MAGENTA:
- ForeColor (magentaColor);
- break;
- case CYAN:
- ForeColor (cyanColor);
- break;
- case WHITE:
- ForeColor (whiteColor);
- break;
- default:
- break;
- }
- }
-
-
- /*
- * MacII_mapcolor
- *
- * change index i in the color map to the appropriate r, g, b, value.
- */
- MacII_mapcolor(i, r, g, b)
- int i;
- int r, g, b;
- {
- if (i > 15)
- usememory = 1;
- }
-
-
- /*
- * MacII_font
- *
- * Set up a hardware font. Return 1 on success 0 otherwise.
- *
- */
- MacII_font(fontfile)
- char *fontfile;
- {
- FontInfo fontInfo;
-
- TextFont (monaco);
- GetFontInfo (&fontInfo);
- vdevice.hheight = fontInfo.ascent + fontInfo.descent + fontInfo.leading;
- vdevice.hwidth = fontInfo.widMax;
-
- return(1);
- }
-
-
- /*
- * MacII_char
- *
- * outputs one char - is more complicated for other devices
- */
- MacII_char(c)
- char c;
- {
- MoveTo (vdevice.cpVx, (int)(vdevice.sizeSy - vdevice.cpVy));
- DrawChar (c);
- }
-
-
- /*
- * MacII_string
- *
- * Display a string at the current drawing position.
- */
- MacII_string(s)
- char s[];
- {
- MoveTo (vdevice.cpVx, (int)(vdevice.sizeSy - vdevice.cpVy));
- CtoPstr (s);
- DrawString ((StringPtr)s);
- PtoCstr ((StringPtr)s);
- }
-
-
- /*
- * MacII_fill
- *
- * fill a polygon
- */
- MacII_fill(n, x, y)
- int n, x[], y[];
- {
- PolyHandle poly;
- int i;
-
- if (n > 128)
- verror("vogl: more than 128 points in a polygon");
-
- poly = OpenPoly ();
- MoveTo (x[0], vdevice.sizeSy - y[0]);
- for (i = 1; i < n; i++) {
- LineTo (x[i], vdevice.sizeSy - y[i]);
- }
- ClosePoly ();
- PaintPoly (poly);
- KillPoly (poly);
-
- vdevice.cpVx = x[n-1];
- vdevice.cpVy = y[n-1];
- }
-
-
- /*
- * MacII_backb
- *
- * swap to memory only drawing (backbuffer) - a little slow but it
- * works on everything. Where we can, we use the frame buffer.
- */
- MacII_backb()
- {
- if (voglWorld == 0L)
- return(-1);
-
- if (isScreen == TRUE)
- {
- SetGWorld ((CGrafPtr)voglWorld, 0L);
- isScreen = FALSE;
- }
-
- return(1);
- }
-
-
- /*
- * MacII_swapb
- *
- * swap the front and back buffers.
- */
- MacII_swapb()
- {
- if (voglWorld == 0L)
- return(-1);
-
- SetPort ((WindowPtr)voglWindow);
- CopyBits ((BitMap*)*voglWorld->portPixMap, (BitMap*)*voglWindow->portPixMap,
- &voglWindow->portRect, &voglWindow->portRect, srcCopy, 0L);
- SetGWorld ((CGrafPtr)voglWorld, 0L);
- }
-
-
- /*
- * MacII_frontb
- *
- * draw in the front buffer
- */
- MacII_frontb()
- {
- SetPort ((WindowPtr)voglWindow);
- isScreen = TRUE;
- }
-
-
- /*
- * the device entry
- */
- static DevEntry MacIIdev = {
- "MacII",
- "monaco",
- "chicago",
- MacII_backb,
- MacII_char,
- MacII_checkkey,
- MacII_clear,
- MacII_color,
- MacII_draw,
- MacII_exit,
- MacII_fill,
- MacII_font,
- MacII_frontb,
- MacII_getkey,
- MacII_init,
- MacII_locator,
- MacII_mapcolor,
- MacII_string,
- MacII_swapb
- };
-
- /*
- * _MacII_devcpy
- *
- * copy the MacII device into vdevice.dev.
- */
- _MacII_devcpy()
- {
- vdevice.dev = MacIIdev;
- }
-